$(function () {
    var $datagrid = $("#datagrid"), $toolbar = $("#toolbar");
    $("#statusId").combobox({
        onChange: function (oldV, newV) {
            $datagrid.datagrid("reload");
        }
    });
    $datagrid.datagrid({
        singleSelect: true,
        fit: true,
        border: false,
        fitColumns: true,
        url: Constant.ctx("/mulanweb/admin/visa_order/search"),
        method: 'post',
        pagination: true,
        toolbar: $toolbar,
        columns: [[
            {field: 'Id', title: '编号'},
            {field: 'Name', title: '姓名'},
            {field: 'OrderNumber', title: '订单号'},
            {field: 'ActuallyPayment', title: '实际支付'},
            {field: 'ShouldPayment', title: '应支付'},
            {field: 'Amount', title: '数量'},
            {field: 'Email', title: '邮箱'},
            {field: 'PhoneNumber', title: '手机'},
            {field: 'StatusName', title: '状态'},
            {field: 'CreateTime', title: '下单时间'},
            {
                field: 'StatusId', title: '操作', formatter: function (v, r, i) {
                if (v == '23' || v == 23) {
                    return '<a href="#" onclick="doneOrder(\'' + r.Id + '\')">结束订单</a>'
                } else {
                    return '无操作';
                }
            }
            },
        ]],
        onBeforeLoad: function (param) {
            param.PageNumber = param.page;
            param.PageSize = param.rows;

            var statusId = $("#statusId").combo("getValue");
            if (statusId && statusId != 0) {
                param.statusId = statusId;
            }

            var orderNumber = $("#orderNumber").val();
            if (orderNumber) {
                param.orderNumber = orderNumber;
            }

            var mobile = $("#mobile").val();
            if (mobile) {
                param.phoneNumber = mobile;
            }
            var nickname = $("#nickname").val();
            if (nickname) {
                param.wechatNickname = nickname;
            }

            var name_user = $("#name").val();
            if (name_user) {
                param.name = name_user;
            }

            return true;
        },
        loadFilter: function (data) {
            var newData = {};
            if (data.ReturnCode == 1) {
                newData.rows = data.DataList;
                if (newData.rows.length > 0) {
                    newData.total = data.TotalRow;
                } else {
                    newData.total = 0;
                }
            }
            return newData;
        },
        onDblClickRow: function (index, rows) {
            showDetail(rows.Id);
        }
    });
    function showDetail(orderId) {
        if (orderId) {
            $.post(Constant.ctx("/mulanweb/admin/visa_order/getDetail"), {orderId: orderId}, function (info) {
                var module = $("#detail").html();
                var compileModule = Template7.compile(module);
                var resultModule = compileModule(info);
                $("#dialogDetail").html(resultModule);
            })
            $("#dialogDetail").dialog({
                title: '详情',
                width: 400,
                height: 500,
                onClose: function () {
                    $("#dialogDetail").html("<h1>请重试</h1>");
                }
            });
        } else {
            Constant.showMessage("请选择对象");
        }
    }
})

function doSearch() {
    $("#datagrid").datagrid("reload");
}

function doneOrder(orderId) {
    var url = Constant.contextUrl + '/mulanweb/admin/visa_order/doneOrder';
    $.post(url, {orderId: orderId}, function (info) {
        Constant.showMessage(info.AlertMessage, info.message, "未知操作");
        $("#datagrid").datagrid("reload");
    });
}